﻿<?php
/*
+-------------------+------------------+------+-----+---------+-------+
| Field             | Type             | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+-------+
| usuCodigo         | int(10) unsigned |      | PRI | 0       |       |
| usuNome           | varchar(50)      | YES  |     | NULL    |       |
| usuSenha          | varchar(8)       | YES  |     | NULL    |       |
| usuApelido        | varchar(20)      | YES  |     | NULL    |       |
| usuEmail          | varchar(50)      | YES  | MUL | NULL    |       |
| usuDataNascimento | date             | YES  |     | NULL    |       |
| usuSexo           | char(1)          | YES  |     | NULL    |       |
| usuRG             | varchar(20)      | YES  |     | NULL    |       |
| usuCPF            | varchar(20)      | YES  |     | NULL    |       |
| usuDataCadastro   | datetime         | YES  |     | NULL    |       |
| usuTelRes         | varchar(14)      | YES  |     | NULL    |       |
| usuTelCel         | varchar(14)      | YES  |     | NULL    |       |
| usuTelCom         | varchar(14)      | YES  |     | NULL    |       |
| usuTelComRamal    | varchar(10)      | YES  |     | NULL    |       |
| endCidade         | varchar(50)      | YES  |     | NULL    |       |
| endBairro         | varchar(50)      | YES  |     | NULL    |       |
| endTipoLogradouro | varchar(20)      | YES  |     | NULL    |       |
| endLogradouro     | varchar(150)     | YES  |     | NULL    |       |
| endNumero         | varchar(20)      | YES  |     | NULL    |       |
| endCep            | varchar(9)       | YES  |     | NULL    |       |
| endComplemento    | varchar(255)     | YES  |     | NULL    |       |
| usuAlterarDados   | char(1)          | YES  |     | NULL    |       |
| usuReceberEmail   | char(1)          | YES  |     | NULL    |       |
| usuLiberarPerfil  | char(1)          | YES  |     | NULL    |       |
| usuStatus         | char(1)          | YES  |     | NULL    |       |
+-------------------+------------------+------+-----+---------+-------+
*/
class UsuarioBean extends BeanUtils {

	var $attributes = array(
		"codigo" 			=> array("name" => "usuCodigo",			"type" => "varchar", "data" => ""),
		"nome" 				=> array("name" => "usuNome", 			"type" => "varchar", "data" => ""),
		"senha" 			=> array("name" => "usuSenha", 			"type" => "varchar", "data" => ""),
		"usuario"			=> array("name" => "usuApelido", 		"type" => "varchar", "data" => ""),
		"email" 			=> array("name" => "usuEmail", 			"type" => "varchar", "data" => ""),
		"dataNascimento"	=> array("name" => "usuDataNascimento", "type" => "varchar", "data" => ""),
		"sexo" 				=> array("name" => "usuSexo", 			"type" => "varchar", "data" => ""),
		"rg" 				=> array("name" => "usuRG", 			"type" => "varchar", "data" => ""),
		"cpf" 				=> array("name" => "usuCPF", 			"type" => "varchar", "data" => ""),
		"dataCadastro" 		=> array("name" => "usuDataCadastro", 	"type" => "varchar", "data" => ""),
		"telefoneRes" 		=> array("name" => "usuTelRes", 		"type" => "varchar", "data" => ""),
		"telefoneCel" 		=> array("name" => "usuTelCel", 		"type" => "varchar", "data" => ""),
		"telefoneCom" 		=> array("name" => "usuTelCom", 		"type" => "varchar", "data" => ""),
		"telefoneComRamal" 	=> array("name" => "usuTelComRamal", 	"type" => "varchar", "data" => ""),
		"cidade" 			=> array("name" => "endCidade", 		"type" => "varchar", "data" => ""),
		"bairro" 			=> array("name" => "endBairro", 		"type" => "varchar", "data" => ""),
		"tipoLogradouro" 	=> array("name" => "endTipoLogradouro", "type" => "varchar", "data" => ""),
		"logradouro"		=> array("name" => "endLogradouro", 	"type" => "varchar", "data" => ""),
		"numero" 			=> array("name" => "endNumero", 		"type" => "varchar", "data" => ""),
		"cep" 				=> array("name" => "endCep", 			"type" => "varchar", "data" => ""),
		"complemento" 		=> array("name" => "endComplemento", 	"type" => "varchar", "data" => ""),
		"alterarDados" 		=> array("name" => "usuAlterarDados", 	"type" => "varchar", "data" => ""),
		"mailing" 			=> array("name" => "usuReceberEmail", 	"type" => "varchar", "data" => ""),
		"liberarPerfil"		=> array("name" => "usuLiberarPerfil", 	"type" => "varchar", "data" => ""),
		"confirmKey" 		=> array("name" => "usuConfirmKey", 	"type" => "varchar", "data" => ""),
		"status" 			=> array("name" => "usuStatus", 		"type" => "varchar", "data" => "")
	);

	function UsuarioBean($codigo = '', $nome = ''){
		$this->setAttribute("codigo", $codigo);
		$this->setAttribute("nome", $nome);
	}
}

class Usuario extends Kernel{

	function get( $instanceDB, $usuarioBean ){

		$commandSql = "Select * From Usuario Where usuCodigo = " . $usuarioBean->getAttribute("codigo");
		$this->setLog("debug", "SELECT_GET_USUARIO_QUERY", $commandSql);
		
		$sql = new Sql();
		$result = $sql->execute($instanceDB, $commandSql);
		if ( $result ){
			$this->setLog("message", "", "Usuário capturado com sucesso.");
			$returnBean = new UsuarioBean();
			return $returnBean->loadBeanByResultSet( mysql_fetch_array( $result ) );
		} else {
			$this->setLog("message", "", "Não foi possível encontrar o usuáro desejado.");
			return 0;
		}

	}

	function delete( $instanceDB, $usuarioBean ){

		$commandSql = "Delete From news_Usuario Where usuCodigo = " . $usuarioBean->getAttribute("codigo");
		$this->setLog("debug", "DELETE_USUARIO_QUERY", $commandSql);
		
		$sql = new Sql();
		$result = $sql->execute($instanceDB, $commandSql);
		if ( $result ){
			$this->setLog("message", "", "Usuaroio deletado com sucesso.");
			return 1;
		} else {
			$this->setLog("message", "", "Não foi possível deletar usuário desejado.");
			return 0;
		}

	}

	function find( $instanceDB, $usuarioBean ){

		$commandSql = "Select * From Usuario Where usuCodigo > 0 ";
		if ( $usuarioBean->getAttribute("nome") != "" ){
			$commandSql .= " AND usuNome like '%" . $usuarioBean->getAttribute("nome") . "%'";
		}
		if ( $usuarioBean->getAttribute("email") != "" ){
			$commandSql .= " AND usuEmail like '%" . $usuarioBean->getAttribute("email") . "%'";
		}
		if ( $usuarioBean->getAttribute("usuario") != "" ){
			$commandSql .= " AND usuApelido like '%" . $usuarioBean->getAttribute("usuario") . "%'";
		}
		if ( $usuarioBean->getAttribute("senha") != "" ){
			$commandSql .= " AND usuSenha = password('" . $usuarioBean->getAttribute("senha") . "')";
		}
		if ( $usuarioBean->getAttribute("confirmKey") != "" ){
			$commandSql .= " AND usuConfirmKey = '" . $usuarioBean->getAttribute("confirmKey") . "'";
		}
		if ( $usuarioBean->getAttribute("status") != "" ){
			$commandSql .= " AND usuStatus = '" . $usuarioBean->getAttribute("status") . "'";
		}
		$commandSql .= " Order By usuNome";

		$this->setLog("debug", "SELECT_FIND_USUARIO_QUERY", $commandSql);
		
		$sql = new Sql();
		$result = $sql->execute($instanceDB, $commandSql);
		if ( $result ){
			$data;
			$usuarioBean = new UsuarioBean();
			while ($row = mysql_fetch_array($result)) {
				$data[] = $usuarioBean->loadBeanByResultSet( $row );
			}
			$this->setLog("message", "", "Usuários capturado com sucesso.");
			return $data;
		} else {
			$this->setLog("message", "", "Não foi possível encontrar Usuário desejado.");
			return false;
		}

	}

	function insert ($instanceDB, $usuarioBean ) {


		$commandSqlCodigo = "Select Max(usuCodigo)+1 as usuCodigo From Usuario";
		$this->setLog("debug", "SELECT_GET_MAX_USUARIO_QUERY", $commandSqlCodigo);
		
		$sql = new Sql();
		$usuarioInsertBean = "";
		$resultCodigo = $sql->execute($instanceDB, $commandSqlCodigo);
		if ( $resultCodigo ){

			$usuarioInsertBean = new UsuarioBean();
			$usuarioInsertBean = $usuarioInsertBean->loadBeanByResultSet( mysql_fetch_array( $resultCodigo ) );

			$commandSql = "INSERT INTO Usuario (";
			$commandSql .= "usuCodigo, usuNome, usuSenha, usuApelido, usuEmail, usuDataNascimento, usuSexo, usuRG, usuCPF, usuDataCadastro, usuTelRes, usuTelCel, usuTelCom, usuTelComRamal, endCidade, endBairro, endTipoLogradouro, endLogradouro, endNumero, endCep, endComplemento, usuAlterarDados, usuReceberEmail, usuLiberarPerfil, usuConfirmKey, usuStatus";
			$commandSql .= ") VALUES(";
			$commandSql .= $usuarioInsertBean->getAttribute("codigo") . ",";
			$commandSql .= "'". $usuarioBean->getAttribute("nome") ."',";
			$commandSql .= "password('". $usuarioBean->getAttribute("senha") ."'),";
			$commandSql .= "'". $usuarioBean->getAttribute("usuario") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("email") ."',";
			$commandSql .= "'". $this->stringToSqlDate( $usuarioBean->getAttribute("dataNascimento") ) ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("sexo") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("rg") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("cpf") ."',";
			$commandSql .= "NOW(),";
			$commandSql .= "'". $usuarioBean->getAttribute("telefoneRes") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("telefoneCel") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("telefoneCom") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("telefoneComRamal") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("cidade") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("bairro") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("tipoLogradouro") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("logradouro") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("numero") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("cep") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("complemento") ."',";
			$commandSql .= "'N',";
			$commandSql .= "'". $usuarioBean->getAttribute("mailing") ."',";
			$commandSql .= "'". $usuarioBean->getAttribute("liberarPerfil") ."',";
			$commandSql .= "'". $this->getRandonKey(25) ."',";
			$commandSql .= "'AC'";
			// AC = Agardando Confirmação
			// EA = Email Autenticado
			// DI = Dados Incompletos
			// CC = Cadastro Completo
			// AN = Acesso Negado
			$commandSql .= ")";
	
			$this->setLog("debug", "INSERT_USUARIO_QUERY", $commandSql);
	
			$sql = new Sql();
			$result = $sql->execute($instanceDB, $commandSql);
			if ( $result ){
				$this->setLog("message", "", "Usuário inserido com sucesso.");
				return $usuarioInsertBean->getAttribute("codigo");
			} else {
				$this->setLog("message", "", "Não foi possível inserir os dados do usuário.");
				return 0;
			}

		} else {
			$this->setLog("message", "", "Erro ao capturar o código do novo usuário.");
			return 0;
		}

		
	}

	function update($instanceDB, $usuarioBean ) {

		$commandSql = "UPDATE Usuario set ";
		$commandSql .= "usuNome = '". $usuarioBean->getAttribute("nome") ."',";
		$commandSql .= "usuSenha = password('". $usuarioBean->getAttribute("senha") ."'),";
		$commandSql .= "usuApelido ='". $usuarioBean->getAttribute("usuario") ."',";
		$commandSql .= "usuEmail = '". $usuarioBean->getAttribute("email") ."',";
		$commandSql .= "usuDataNascimento = '". $usuarioBean->getAttribute("dataNascimento") ."',";
		$commandSql .= "usuSexo = '". $usuarioBean->getAttribute("sexo") ."',";
		$commandSql .= "usuRG = '". $usuarioBean->getAttribute("rg") ."',";
		$commandSql .= "usuCPF = '". $usuarioBean->getAttribute("cpf") ."',";
		$commandSql .= "usuTelRes = '". $usuarioBean->getAttribute("telefoneRes") ."',";
		$commandSql .= "usuTelCel = '". $usuarioBean->getAttribute("telefoneCel") ."',";
		$commandSql .= "usuTelCom = '". $usuarioBean->getAttribute("telefoneCom") ."',";
		$commandSql .= "usuTelComRamal = '". $usuarioBean->getAttribute("telefoneComRamal") ."',";
		$commandSql .= "endCidade = '". $usuarioBean->getAttribute("cidade") ."',";
		$commandSql .= "endBairro = '". $usuarioBean->getAttribute("bairro") ."',";
		$commandSql .= "endTipoLogradouro = '". $usuarioBean->getAttribute("tipoLogradouro") ."',";
		$commandSql .= "endLogradouro = '". $usuarioBean->getAttribute("logradouro") ."',";
		$commandSql .= "endNumero = '". $usuarioBean->getAttribute("numero") ."',";
		$commandSql .= "endCep = '". $usuarioBean->getAttribute("cep") ."',";
		$commandSql .= "endComplemento = '". $usuarioBean->getAttribute("complemento") ."',";
		$commandSql .= "usuAlterarDados = '". $usuarioBean->getAttribute("alterarDados") ."',";
		$commandSql .= "usuReceberEmail = '". $usuarioBean->getAttribute("mailing") ."',";
		$commandSql .= "usuLiberarPerfil = '". $usuarioBean->getAttribute("liberarPerfil") ."',";
		$commandSql .= "usuConfirmKey = '". $usuarioBean->getAttribute("confirmKey") ."',";
		$commandSql .= "usuStatus = '". $usuarioBean->getAttribute("status") ."'";
		$commandSql .= " Where usuCodigo = " . $usuarioBean->getAttribute("codigo");
		
		$this->setLog("debug", "UPDATE_USUARIO_QUERY", $commandSql);

		$sql = new Sql();
		$result = $sql->execute($instanceDB, $commandSql);
		if ( $result ){
			$this->setLog("message", "", "Usuário alterado com sucesso.");
			return true;
		} else {
			$this->setLog("message", "", "Não foi possível alterar os dados do usuário.");
			return false;
		}
	}
}

?>
